package org.example.cascader.service;

import org.example.cascader.domain.BaseZyjbqk;
import org.example.cascader.domain.ZyTreeDTO;
import org.example.cascader.mapper.BaseZyjbqkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ZyTreeService {
    @Autowired
    private BaseZyjbqkMapper baseZyjbqkMapper;

    /**
     * 构建完整三级树形结构
     */
    public List<ZyTreeDTO> buildFullTree() {
        // 1. 获取所有专业数据
        List<BaseZyjbqk> allItems = baseZyjbqkMapper.selectList(null);

        // 2. 构建树形结构
        return buildThreeLevelTree(allItems);
    }

    private List<ZyTreeDTO> buildThreeLevelTree(List<BaseZyjbqk> allItems) {
        // 3. 按授予学位门类分组(祖父节点)
        Map<String, List<BaseZyjbqk>> syxwmlMap = allItems.stream()
                .filter(item -> item.getSyxwml() != null)
                .collect(Collectors.groupingBy(BaseZyjbqk::getSyxwml));

        List<ZyTreeDTO> result = new ArrayList<>();

        // 4. 构建祖父节点
        syxwmlMap.forEach((syxwml, items) -> {
            ZyTreeDTO grandparent = ZyTreeDTO.convert(items.get(0), 1);

            // 5. 构建父节点(按专业门类分组)
            Map<String, List<BaseZyjbqk>> zymcMap = items.stream()
                    .filter(item -> item.getZymc() != null)
                    .collect(Collectors.groupingBy(BaseZyjbqk::getZymc));

            List<ZyTreeDTO> parentNodes = new ArrayList<>();
            zymcMap.forEach((zymc, subItems) -> {
                ZyTreeDTO parent = ZyTreeDTO.convert(subItems.get(0), 2);

                // 6. 构建子节点
                List<ZyTreeDTO> children = subItems.stream()
                        .map(item -> ZyTreeDTO.convert(item, 3))
                        .sorted(Comparator.comparing(ZyTreeDTO::getLabel))
                        .collect(Collectors.toList());

                parent.setChildren(children);
                parentNodes.add(parent);
            });

            grandparent.setChildren(
                    parentNodes.stream()
                            .sorted(Comparator.comparing(ZyTreeDTO::getLabel))
                            .collect(Collectors.toList())
            );
            result.add(grandparent);
        });

        return result.stream()
                .sorted(Comparator.comparing(ZyTreeDTO::getLabel))
                .collect(Collectors.toList());
    }

    /**
     * 按层级查询
     */
    public List<ZyTreeDTO> getByLevel(int level) {
        List<BaseZyjbqk> items;
        switch(level) {
            case 1: // 祖父节点
                items = baseZyjbqkMapper.selectDistinctSyxwml();
                break;
            case 2: // 父节点
                items = baseZyjbqkMapper.selectDistinctZymc();
                break;
            case 3: // 子节点
                items = baseZyjbqkMapper.selectList(null);
                break;
            default:
                return Collections.emptyList();
        }

        return items.stream()
                .map(item -> ZyTreeDTO.convert(item, level))
                .distinct()
                .sorted(Comparator.comparing(ZyTreeDTO::getLabel))
                .collect(Collectors.toList());
    }

    /**
     * 获取子节点
     */
    public List<ZyTreeDTO> getChildren(String parentValue, int parentLevel) {
        List<BaseZyjbqk> items;
        switch(parentLevel) {
            case 1: // 祖父节点查父节点
                items = baseZyjbqkMapper.selectBySyxwml(parentValue);
                break;
            case 2: // 父节点查子节点
                items = baseZyjbqkMapper.selectByZymc(parentValue);
                break;
            default:
                return Collections.emptyList();
        }

        int childLevel = parentLevel + 1;
        return items.stream()
                .map(item -> ZyTreeDTO.convert(item, childLevel))
                .distinct()
                .sorted(Comparator.comparing(ZyTreeDTO::getLabel))
                .collect(Collectors.toList());
    }
}
